package com.icbc.services;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.icbc.model.Document;

public class DocumentService {
	
	@Autowired
	public MongoOperations mongoOperation;
	
	public Collection<Document> getAllDocuments(){
		return mongoOperation.findAll(Document.class);
	}
	
	public void deleteDocumentById(String id){
		Query searchQuery = new Query(Criteria.where("id").in(id));
		mongoOperation.remove(searchQuery,Document.class);
	}
	
	public Collection<Document> getDocumentByTags(Collection<String> tags){
		Query searchQuery = new Query(Criteria.where("tags").in(tags));
		return mongoOperation.find(searchQuery, Document.class);
	}
	
	public Document getDocumentById(String id){
		Query searchQuery = new Query(Criteria.where("id").in(id));
		return mongoOperation.find(searchQuery, Document.class).get(0);
	}
	
	public void saveDocument(Document doc){
		mongoOperation.save(doc);
	}
}

 
